/**
 * Utils to parse Google maps getDirections() result
 */
define([
	"dojo"
],
    
function( dojo )
{
	var Services = 
	{
		getCoordinates : function(response) 
		{
	 		return this.decodeLine(response.routes[0].overview_polyline.points);
		},

		// This function is from Google's polyline utility.
		decodeLine : function(encoded) 
		{
		  var len = encoded.length;
		  var index = 0;
		  var array = [];
		  var lat = 0;
		  var lng = 0;
		
		  while (index < len) {
		    var b;
		    var shift = 0;
		    var result = 0;
		    do {
		      b = encoded.charCodeAt(index++) - 63;
		      result |= (b & 0x1f) << shift;
		      shift += 5;
		    } while (b >= 0x20);
		    var dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
		    lat += dlat;
		
		    shift = 0;
		    result = 0;
		    do {
		      b = encoded.charCodeAt(index++) - 63;
		      result |= (b & 0x1f) << shift;
		      shift += 5;
		    } while (b >= 0x20);
		    var dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
		    lng += dlng;
		
		    array.push([lat * 1e-5, lng * 1e-5]);
		  }
		
		  return array;
		}
	}

	return Services;

});
